﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.UI.WebControls;
using System.IO;

namespace XiaoSe.Web.UI.WebControls
{
    /// <summary>
    /// 上传文件帮助类
    /// </summary>
    public class FileUploadHelper
    {
        /// <summary>
        /// 读取文件前两个字节的值来判断是不是合法的文件类型，文件的大小至少要两个字节
        /// </summary>
        /// <param name="file">FileUpload控件</param>
        /// <param name="allowFile">允许上传文件前两个字节的合法值</param>
        /// <returns>布尔值</returns>
        public static bool IsAllowFile(FileUpload file, IEnumerable<string> allowFile)
        {
            var br = new BinaryReader(file.FileContent);
            try
            {
                byte Byte = br.ReadByte();
                var value = Byte.ToString(CultureInfo.InvariantCulture);
                Byte = br.ReadByte();
                value += Byte.ToString(CultureInfo.InvariantCulture);
                if (allowFile.Any(allow => value == allow))
                {
                    br.Close();
                    return true;
                }
            }
            catch
            {
                br.Close();
            }
            return false;
        }
        /// <summary>
        /// 待上传的文件是否同过
        /// </summary>
        /// <param name="file">FileUpload空间</param>
        /// <param name="allowExe">允许上传的后缀名，记得带.</param>
        /// <param name="maxSize">最大大小</param>
        /// <returns></returns>
        public static FileUploadRutrunInfo IsAllowFile(FileUpload file, IEnumerable<string> allowExe, int maxSize)
        {
            if (!file.HasFile)
            {
                return FileUploadRutrunInfo.Null;
            }
            var length = (int)file.FileContent.Length;
            var fileName = file.FileName.ToLower();
            if (length > maxSize)
            {
                return FileUploadRutrunInfo.Over;
            }
            var extension = Path.GetExtension(fileName);
            return !allowExe.Contains(extension.ToLower()) ? FileUploadRutrunInfo.NotAllow : FileUploadRutrunInfo.Ok;
        }


        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="sFile"></param>
        /// <param name="saveFilePath"></param>
        /// <param name="allowExe">允许上传的文件后缀(带.)</param>
        /// <param name="maxSize">允许文件最大大小</param>
        public static FileUploadRutrunInfo UpLoad(Stream sFile, string saveFilePath, IEnumerable<string> allowExe, int maxSize)
        {
            var length = (int)sFile.Length;
            if (length > maxSize)
            {
                return FileUploadRutrunInfo.Over;
            }
            string sExe = Path.GetExtension(saveFilePath);
            if (!allowExe.Contains(sExe))
            {
                return FileUploadRutrunInfo.NotAllow;
            }
            var fs = new FileStream(saveFilePath, FileMode.Create);
            var b = new byte[length];
            sFile.Read(b, 0, length);
            fs.Write(b, 0, length);
            sFile.Close();
            fs.Close();
            return FileUploadRutrunInfo.Ok;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">FileUpload控件</param>
        /// <param name="saveFilePath"></param>
        /// <param name="allowExe">允许上传的文件后缀(带.)</param>
        /// <param name="maxSize">允许文件最大大小</param>
        public static FileUploadRutrunInfo UpLoad(FileUpload file, string saveFilePath, string[] allowExe, int maxSize)
        {
            FileUploadRutrunInfo info = IsAllowFile(file, allowExe, maxSize);
            if (info == FileUploadRutrunInfo.Ok)
            {
                file.SaveAs(saveFilePath);
            }
            return info;
        }
    }
    public enum FileUploadRutrunInfo
    {
        Ok = 1,
        NotAllow = 2,
        Null = 4,
        Over = 8
    }
}
